#!/bin/bash

if [ `basename "$PWD"` != "Report" ]; then
  echo "Error: this scripts must be run on the 'Report' folder"
  exit 1
fi

PDF="Driver-report.pdf"
folder_diagrams="./diagrams"

if [ ! -d "$folder_diagrams" ]; then
  mkdir "$folder_diagrams"
fi

# Clean the tempory files
if [ $# -eq 1 ]; then
  if [ $1 == "-c" ]; then
    echo "Cleanning temporary files..."
    mv report.tex bak_report.tex
    rm report.*
    mv bak_report.tex report.tex
    rm -rf diagrams
  
    exit 0
  fi
fi
# Convert images to pdf format
echo "Converting images..."
cd "$folder_diagrams"
for file in $(find ../../Model/ -name "*.dia"); do
  dia -t eps "$file" > /dev/null
done
for file in $(find . -name "*.eps"); do
  epstopdf "$file"
  rm "$file"
done
cd ..

# Generate report
echo "Generating report..."
pdflatex report.tex > /dev/null
bibtex report > /dev/null
makeglossaries report > /dev/null
pdflatex report.tex > /dev/null
pdflatex report.tex > /dev/null
mv report.pdf "$PDF"
